package cn.tool.lwd;


import com.google.common.base.CaseFormat;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author lwd
 * @date 2018/12/17 14:13
 */
public class FiltterReplaceCodeUtil {

    private Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);

    private StringTemplateLoader stringLoader = new StringTemplateLoader();

    public void init(String resourcesPath) {
        //遍历resources
        try {
            configuration.setDefaultEncoding("UTF-8");
            initTemplate(resourcesPath);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void initTemplate(String path) throws FileNotFoundException, UnsupportedEncodingException {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        stringLoader.putTemplate(file2.getAbsolutePath(),file2.getAbsolutePath());
                        configuration.setTemplateLoader(stringLoader);
                        initTemplate(file2.getAbsolutePath());
                    } else {
                        System.out.println("文件:" + file2.getAbsolutePath());
                        InputStream inputStream = new FileInputStream(file2);
                        String collect = new BufferedReader(new InputStreamReader(inputStream,"UTF-8")).lines().collect(Collectors.joining(System.lineSeparator()));
                        stringLoader.putTemplate(file2.getAbsolutePath(),collect);
                        configuration.setTemplateLoader(stringLoader);
                    }
                }
            }
        } else {
            System.out.println(path + "222文件不存在!");
        }
    }


    /**
     * 根据模板获取字符串
     * @param name 模板名字
     * @param model 模板参数
     * @return
     */
    public String getContentByTemplate(String name, Object model) {
        try {
            Template template = configuration.getTemplate(name);
            return processTemplateIntoString(template,model);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据模板获取字符串
     * @param content 模板名字
     * @param model 模板参数
     * @return
     */
    public String getContentByContent(String content, Object model) {
        try {
            String templateName = UUID.randomUUID().toString();
            stringLoader.putTemplate(templateName,content);
            configuration.setTemplateLoader(stringLoader);
            Template template = configuration.getTemplate(templateName);
            return processTemplateIntoString(template,model);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 使用模板获取渲染结果
     * @param template
     * @param model
     * @return
     * @throws IOException
     * @throws TemplateException
     */
    private String processTemplateIntoString(Template template,Object model) throws IOException, TemplateException {
        StringWriter result = new StringWriter(1024);
        template.process(model, result);
        return result.toString();
    }

    public static void main(String[] args) {

        System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test_data"));//testData
        System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data"));//testData
        System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data"));//TestData

        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "testdata"));//testdata
        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "TestData"));//test_data
        System.out.println(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "testData"));//test-data


    }

}
